package com.appkqxs.paser;

import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.appkqxs.enity.YoutubeEntity;

public class VimeoLoad {

	public static ArrayList<YoutubeEntity> listYoutube = new ArrayList<YoutubeEntity>();
	private static ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
	private static ArrayList<String> listUser = new ArrayList<String>();
	
	private String[] getTitleAndDescription(String url) {
		
		String[] str = new String[2];
		try {
			Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get();
			Elements els = doc.select("meta[name=description]");
			str[0] = new String(doc.title().trim());
			str[1] = new String(els.attr("content").trim());
		} catch (Exception e) {
			System.out.println("Error - Link: "+ url + " - " +e.getMessage());
			str[0] = new String();
			str[1] = new String();
		}
		
		return str;
	}
	
	public void getMoreVideo(String url)  {
		try {
			if(!url.equals("http://vimeo.com"))  {
				Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get();
				Elements els;
				if(doc.select("a[href~=^(/\\d+)$]:has(img)").text().equals("")) {
					 els = doc.select("a[href~=^/(channels|groups)/\\w+(/videos)?(/\\d+)$]:has(img)");
				} else {
					 els = doc.select("a[href~=^(/\\d+)$]:has(img)");
				}
				String linkPage = "http://vimeo.com";
				try {
					Elements elsPagination = doc.select("div#pagination.pagination ol li a.selected");
					String pageCurrent = elsPagination.attr("data-page").trim();
					int page = Integer.parseInt(pageCurrent) + 1;
					System.out.println(page);
					linkPage =  linkPage+elsPagination.attr("href").toString().replace("page:"+pageCurrent, ("page:"+page));
					System.out.println(linkPage);
				} catch (Exception e) {
					// TODO: handle exception
				}
				getListChilrent(els);
			    getMoreVideo(linkPage);
			    
			}
		} catch (Exception e) {
			System.out.println("Error Get More: "+e.getMessage());
		}
	}
	
	public void getListChilrent(Elements els) throws Exception {
		for (Element element : els) {
			String link;
			Pattern replace = Pattern.compile("^/(channels|groups)/\\w+(/videos)?(/\\d+)$");
		    Matcher matcher = replace.matcher(element.attr("href"));
			if(matcher.matches()) {
				link = "http://vimeo.com"+matcher.group(3);
			} else {
				link = "http://vimeo.com"+element.attr("href");
			}
			
			String img = element.select("img").attr("src");
			//String title = element.select("p.title").text();
			String[] titleDesctiption = getTitleAndDescription(link);
			System.out.println("Link:" + link);
			System.out.println("Image:" + img);
			System.out.println("Title:" + titleDesctiption[0]);
			System.out.println("Description:" + titleDesctiption[1]);
			
		}
		
	}
	public void getListYou(String url) throws Exception {
			Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get();
			Elements els;
			if(doc.select("a[href~=^(/\\d+)$]:has(img)").text().equals("")) {
				 els = doc.select("a[href~=^/(channels|groups)/\\w+(/videos)?(/\\d+)$]:has(img)");
			} else {
				 els = doc.select("a[href~=^(/\\d+)$]:has(img)");
			}
			String linkPage = "http://vimeo.com";
			try {
				Elements elsPagination = doc.select("div#pagination.pagination ol li a.selected");
				String pageCurrent = elsPagination.attr("data-page").trim();
				int page = Integer.parseInt(pageCurrent) + 1;
				System.out.println(page);
				linkPage =  linkPage+elsPagination.attr("href").toString().replace("page:"+pageCurrent, ("page:"+page));
				System.out.println(linkPage);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			for (Element element : els) {
				String link;
				Pattern replace = Pattern.compile("^/(channels|groups)/\\w+(/videos)?(/\\d+)$");
			    Matcher matcher = replace.matcher(element.attr("href"));
				if(matcher.matches()) {
					link = "http://vimeo.com"+matcher.group(3);
				} else {
					link = "http://vimeo.com"+element.attr("href");
				}
				
				String img = element.select("img").attr("src");
				//String title = element.select("p.title").text();
				String[] titleDesctiption = getTitleAndDescription(link);
				System.out.println("Link:" + link);
				System.out.println("Image:" + img);
				System.out.println("Title:" + titleDesctiption[0]);
				System.out.println("Description:" + titleDesctiption[1]);
				
			}
			getMoreVideo(linkPage);
		
	}
	public void getQueue() {
		
		concurrentLinkedQueue.add("http://www.youtube.com/user/TaylorSwiftVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/ShayneWardVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/BridgitMendlerVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/SelenaGomezVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/TRESEMME/videos");
		
		
	}
	class ThreadGetLink implements Runnable {
		private String link;
		public ThreadGetLink(String link) {
			this.link = link;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println(link);
			try {
				getListYou(link);
			} catch (Exception e) {
				System.out.println("Error: "+e.getMessage());
			}
			
		}
		
	}
	public void threadQueue() {
		getQueue();
		
		int threadSize = 5;
		while(true) {
			try {
				if(concurrentLinkedQueue.size() == 0) {
					System.out.println("Not found Queue");
				} else {
					
					for (int i = 0; i < threadSize; i++) {
						String link = concurrentLinkedQueue.poll();
						if(Thread.activeCount() <= threadSize) {
							Thread thread = new Thread(new ThreadGetLink(link));
							thread.start();
						}
					}
				}
				System.out.println("Thread : "+Thread.activeCount());
				System.out.println("ConcurrentLinkedQueue: "+ concurrentLinkedQueue.size());
				System.out.println("ListYoutube: "+listYoutube.size());
				System.out.println("ListUser: "+listUser.size());
			} catch (Exception e) {
				System.out.println("Error: "+e.getMessage());
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				System.out.println("Error Thread Queue: "+e.getMessage());
			}
		}
	}
	public static void main(String[] args) throws Exception {

		//new VimeoLoad().threadQueue();
		new VimeoLoad().getListYou("http://vimeo.com/66017917");
		//new VimeoLoad().getMoreVideo("http://vimeo.com/categories/films/videos/page:10/sort:relevant");
		

	}

}
